package com.college.common.response;

import com.college.common.constant.ExceptionCodeEnum;
import com.college.common.constant.ExceptionHandler;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 公共返回对象
 */
@Data
public class CommonResult<T> {

	private Integer code;

	private String message;

	private T data;

	/**
	 * 预留对象，这个字段本来是不需要的，但是因为接口的业务拓展，导致需要修改原本的结构（麻烦），为了省掉一些小修改操作，增加解耦，就新增了这个对象
	 */
	private Object reservedData;

	public CommonResult(Integer code) {
		this.code = code;
	}

	protected CommonResult(int code, String message, T data) {
		this.code = code;
		this.message = message;
		this.data = data;
	}


	public CommonResult(Integer code, String message, T data, Object reservedData) {
		this.code = code;
		this.message = message;
		this.data = data;
		this.reservedData = reservedData;
	}

	/**
	 * 成功返回结果
	 */
	public static <T> CommonResult<T> success() {
		return new CommonResult<T>(ExceptionCodeEnum.SUCCESS.getCode(), null, null);
	}

	/**
	 * 成功返回结果
	 */
	public static <T> CommonResult<T> success(String message) {
		return new CommonResult<T>(ExceptionCodeEnum.SUCCESS.getCode(), message, null);
	}

	/**
	 * 成功返回结果
	 *
	 * @param data 获取的数据
	 */
	public static <T> CommonResult<T> success(T data) {
		return new CommonResult<T>(ExceptionCodeEnum.SUCCESS.getCode(), null, data);
	}

	public static <T> CommonResult<T> success(T data, Object reservedData) {
		return new CommonResult<T>(ExceptionCodeEnum.SUCCESS.getCode(), null, data, reservedData);
	}


	/**
	 * 成功返回结果
	 *
	 * @param record 获取的数据
	 */
	public static CommonResult<Map<String, Object>> success(MyRecord record) {
		return new CommonResult<>(ExceptionCodeEnum.SUCCESS.getCode(), null, record.getColumns());
	}

	/**
	 * 成功返回结果
	 *
	 * @param recordList 获取的数据
	 */
	public static CommonResult<List<Map<String, Object>>> success(List<MyRecord> recordList) {
		List<Map<String, Object>> list = new ArrayList<>();
		recordList.forEach(i -> {
			list.add(i.getColumns());
		});
		return new CommonResult<>(ExceptionCodeEnum.SUCCESS.getCode(), ExceptionCodeEnum.SUCCESS.getMessage(), list);
	}

	/**
	 * 成功返回结果
	 *
	 * @param data    获取的数据
	 * @param message 提示信息
	 */
	public static <T> CommonResult<T> success(T data, String message) {
		return new CommonResult<T>(ExceptionCodeEnum.SUCCESS.getCode(), message, data);
	}

	/**
	 * 失败返回结果
	 *
	 * @param errorCode 错误码
	 */
	public static <T> CommonResult<T> failed(ExceptionHandler errorCode) {
		return new CommonResult<T>(errorCode.getCode(), errorCode.getMessage(), null);
	}

	/**
	 * 失败返回结果
	 *
	 * @param errorCode 错误码
	 * @param message   错误信息
	 */
	public static <T> CommonResult<T> failed(ExceptionHandler errorCode, String message) {
		return new CommonResult<T>(errorCode.getCode(), message, null);
	}

	/**
	 * 失败返回结果
	 *
	 * @param message 提示信息
	 */
	public static <T> CommonResult<T> failed(String message) {
		return new CommonResult<T>(ExceptionCodeEnum.FAILED.getCode(), message, null);
	}

	public static <T> CommonResult<T> failed(String message, Object reservedData) {
		return new CommonResult<T>(ExceptionCodeEnum.FAILED.getCode(), message, null, reservedData);
	}

	public static <T> CommonResult<T> failed(String message,T data, Object reservedData) {
		return new CommonResult<T>(ExceptionCodeEnum.FAILED.getCode(), message, data, reservedData);
	}



	/**
	 * 失败返回结果
	 *
	 * @param data
	 * @param <T>
	 * @return
	 */
	public static <T> CommonResult<T> failed(T data) {
		return new CommonResult<T>(ExceptionCodeEnum.FAILED.getCode(), null, data);
	}

	/**
	 * 失败返回结果
	 */
	public static <T> CommonResult<T> failed() {
		return failed(ExceptionCodeEnum.FAILED);
	}

	/**
	 * 参数验证失败返回结果
	 */
	public static <T> CommonResult<T> validateFailed() {
		return failed(ExceptionCodeEnum.VALIDATE_FAILED);
	}

	/**
	 * 参数验证失败返回结果
	 *
	 * @param message 提示信息
	 */
	public static <T> CommonResult<T> validateFailed(String message) {
		return new CommonResult<T>(ExceptionCodeEnum.VALIDATE_FAILED.getCode(), message, null);
	}


	public static <T> CommonResult<T> validateFailed(String message, Object reservedData) {
		return new CommonResult<T>(ExceptionCodeEnum.VALIDATE_FAILED.getCode(), message, null, reservedData);
	}
	public static <T> CommonResult<T> validateFailedSetData(T data) {
		return new CommonResult<T>(ExceptionCodeEnum.VALIDATE_FAILED.getCode(), null, data);
	}

	/**
	 * 未登录返回结果
	 */
	public static <T> CommonResult<T> unauthorized(T data) {
		return new CommonResult<T>(ExceptionCodeEnum.UNAUTHORIZED.getCode(), ExceptionCodeEnum.UNAUTHORIZED.getMessage(), data);
	}

	/**
	 * 未登录返回结果
	 */
	public static <T> CommonResult<T> unauthorized() {
		return new CommonResult<T>(ExceptionCodeEnum.UNAUTHORIZED.getCode(), ExceptionCodeEnum.UNAUTHORIZED.getMessage(), null);
	}

	/**
	 * 没有权限查看
	 */
	public static <T> CommonResult<T> forbidden() {
		return new CommonResult<T>(ExceptionCodeEnum.FORBIDDEN.getCode(), ExceptionCodeEnum.FORBIDDEN.getMessage(), null);
	}

	/**
	 * 未授权返回结果
	 */
	public static <T> CommonResult<T> forbidden(T data) {
		return new CommonResult<T>(ExceptionCodeEnum.FORBIDDEN.getCode(), ExceptionCodeEnum.FORBIDDEN.getMessage(), data);
	}

	public static <T> CommonResult<T> create(Integer code, String message, T data) {
		return new CommonResult<T>(code, message, data);
	}

	public static <T> CommonResult<T> create(Integer code, String message) {
		return new CommonResult<T>(code, message, null);
	}

	public static <T> CommonResult<T> create(Integer code) {
		return create(code, "");
	}

	public static CommonResult successData(String data) {
		return success((Object) data);
	}

	public static CommonResult successData(String data, String message) {
		return success((Object) data, message);
	}

	public boolean isSuccess() {
		return this.code.equals(ExceptionCodeEnum.SUCCESS.getCode());
	}

	public boolean isFailed() {
		return this.code.equals(ExceptionCodeEnum.FAILED.getCode());
	}

	public boolean isValidate() {
		return this.code.equals(ExceptionCodeEnum.VALIDATE_FAILED.getCode());
	}

	public boolean isNotSuccess() {
		return !isSuccess();
	}
}
